<?php

namespace App\Models;

use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class User extends Base
{

    //分页获取列表数据
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where);
            if(!empty($whereDates)){
                $model = $model->whereDate('created_at','>=',$whereDates[0])->whereDate('created_at','<',$whereDates[1]);
            }
            $model=$model->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    /**
     * 新增单个用户
     */
    public function userAdd($data=array()){
        try {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $id = $this->insertGetId($data);
            if($id){
                return [
                    'code'=>0,
                    'data'=>$id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑单个用户
    public function doUpdate($data){
        try {
            $model = self::find($data['id']);
            foreach ($data as $key=>$v){
                if($v){
                    $model->$key = $v;
                }else{
                    if($key=='total'){
                        $model->$key = 0;
                    }
                }
            }
            $model->token = $model->token?$model->token:'';
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'更新失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //改变用户状态
    public function changeStatus($data){
        try {
            //存储数据
            $model = self::find($data['id']);
            if($data['status']){
                $model->deleted_at = null;
            }else{
                $model->deleted_at = date('Y-m-d H:i:s');
            }
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$model->id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'操作失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //根据搜索条件获取所有用户数据
    public function getAllList($where=[],$whereDates){
        try{
            $model = $this->where($where);
            if(!empty($whereDates)){
                $model = $model->whereDate('created_at','>=',$whereDates[0])->whereDate('created_at','<',$whereDates[1]);
            }
            $model=$model->orderBy('created_at','desc');
            $result = $model->get()->toArray();
            return [
                'code'=>0,
                'data'=>$result
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //批量添加用户
    public function userAddAll($data,$rules,$message){
        try {
            $errorData = [];
            $now = date('Y-m-d H:i:s');
            foreach ($data as $key=>$item){
                $validator = Validator::make($item,$rules,$message);
                if($validator->fails()){
                    $error = $validator->errors()->first();
                    $errorData[$key]['data']=$item;
                    $errorData[$key]['errmsg']=$error;
                }else{
                    $item['password'] = Hash::make('123456');
                    $unid = uniqid();
                    $item['spreadid'] =substr($unid.$key,0,17) ;
                    $item['token'] = '';
                    $item['created_at'] = $now;
                    $item['updated_at'] = $now;
                    $this->insertGetId($item);
                }
            }
            return [
                'code'=>0,
                'data'=>$errorData
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //设置登录密码
    public function passwordSet($data){
        try {
            $user = self::find($data['id']);
            $user->password = Hash::make($data['password']);
            $res = $user->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>''
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'修改失败请重试'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //设置支付密码
    public function payPassSet($data){
        try {
            $user = self::find($data['id']);
            $user->paypass = Hash::make($data['password']);
            $res = $user->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>''
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'修改失败请重试'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //用户登录
    public function login($data,$lang='zn'){
        try {
            $user = $this->where('email',$data['email'])->select('id','spreadid','email','username','username_zn','password','contact','total','birthday','token')->first();
            if(!$user){
                return [
                    'code'=>1,
                    'message'=>$this->lang['USER_NOT'][$lang]
                ];
            }
            if (!Hash::check($data['password'], $user->password)) {
                return [
                    'code'=>1,
                    'message'=>$this->lang['PASS_ERROR'][$lang]
                ];
            }
            $newroken = $this->setToken($user->id);
            $user->token = $newroken;
            $user->save();
            $return = $user->toArray();
            //判断有没被推荐
            $bspred = (new SpreadRecord())->where('uid',$user->id)->value('spreadid');
            if($bspred){
                $return['spread'] = $bspred;
            }
            return [
                'code'=>0,
                'data'=>$return
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }
}
